#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int lastStoneWeight(vector<int>& stones)
    {
        int n = stones.size();

        priority_queue<int> head;
        for (auto x : stones) head.push(x);

        while (head.size() > 1)
        {
            int x = head.top(); head.pop();
            int y = head.top(); head.pop();
            if (x > y) head.push(x - y);
        }
        return head.size() ? head.top() : 0;
    }
};